package com.frinder.web;

import com.frinder.base.Result;
import com.frinder.base.ResultGenerator;
import com.frinder.cglib.ExtraProperties;
import com.frinder.cglib.ExtraProperty;
import com.frinder.model.User;
import com.frinder.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by frinder on 2017/10/13.
 */
@RestController
@RequestMapping("/user2")
public class User2Controller {
    @Resource
    private UserService userService;

    @ApiOperation(value = "根据 id 查询 User")
//    @ExtraProperty(name = "address", value = "GZ of China !")
//    @ExtraProperties(extraProperties = {
//            @ExtraProperty(name = "address", value = "GZ of China!"),
//            @ExtraProperty(name = "phone", value = "18888888888")
//    })
    @ExtraProperties(extraProperties = {
            @ExtraProperty(
                    name = "address",
                    refSid = "id",
                    tableName = "user_info",
                    fieldName = "address",
                    refFieldName = "user_sid"
            ),
            @ExtraProperty(
                    name = "phone",
                    refSid = "id",
                    tableName = "user_info",
                    fieldName = "phone",
                    refFieldName = "user_sid"
            )
    })
    @GetMapping("/detail")
    public Result detail(@RequestParam Long id) {
        User user = userService.findById(id);
        return ResultGenerator.genSuccessResult(user);
    }

    @ApiOperation(value = "分页查询 User")
//    @ExtraProperty(name = "address", value = "GZ of China !")
//    @ExtraProperties(extraProperties = {
//            @ExtraProperty(name = "address", value = "GZ of China!"),
//            @ExtraProperty(name = "phone", value = "18888888888")
//    })
    @ExtraProperties(extraProperties = {
            @ExtraProperty(
                    name = "address",
                    refSid = "id",
                    tableName = "user_info",
                    fieldName = "address",
                    refFieldName = "user_sid"
            ),
            @ExtraProperty(
                    name = "phone",
                    refSid = "id",
                    tableName = "user_info",
                    fieldName = "phone",
                    refFieldName = "user_sid"
            )
    })
    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        PageHelper.startPage(page, size);
        List<User> list = userService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }
}
